#include <vector>
#include <unordered_map>
#include <string>

using namespace std;
class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int , vector<int>> map;
        for(int i=0;i<nums.size();i++)
        {
            map[nums[i]].push_back(i);
        }
        for(int i =0 ;i<nums.size();i++)
        {
            if(map[nums[i]].size()>=2)
            {
                vector<int> tmp = map[nums[i]];
                int start = 0;
                int end = 1;
                while(end < tmp.size())
                {
                    if(tmp[end] - tmp[start] <= k){
                        return true;
                    }
                    else{
                        end++;
                        start++;
                    }
                }
            }

        }
            return false;
    }
};